package org.sang.controller;

import com.github.pagehelper.Page;
import com.google.common.collect.Maps;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.sang.bean.OrderConsume;
import org.sang.dto.AddOrderConsumeReqDto;
import org.sang.service.IOrderConsumeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**
 * 〈一句话描述〉
 * 〈功能详细描述〉
 *
 * @author Naughty
 * @version 2018/4/4
 * @see OrderConsumeController
 * @since 1.0
 */

@RestController
@RequestMapping("/order/consume")
public class OrderConsumeController {
    @Autowired
    private IOrderConsumeService iOrderConsumeService;


    @PostMapping(value = "/add", produces = {MediaType.APPLICATION_JSON_VALUE})
    @ApiOperation(value = "新增消费记录", notes = "新增消费记录")
    public ResponseEntity<OrderConsume> addOrderConsume(@RequestBody OrderConsume orderConsume) {
        return ResponseEntity.ok(iOrderConsumeService.insertOrderConsume(orderConsume));
    }

    @DeleteMapping(value = "/delete/{consumeId}")
    @ApiOperation(value = "删除消费记录", notes = "删除消费记录")
    public ResponseEntity deleteOrderConsume(
        @ApiParam(value = "记录编号",required = true,name = "consumeId")
        @PathVariable("consumeId") String consumeId) {
        iOrderConsumeService.deleteOrderConsume(consumeId);
        return ResponseEntity.ok().build();
    }


    @PostMapping(value = "/update",produces = {MediaType.APPLICATION_JSON_VALUE})
    @ApiOperation(value = "修改消费记录", notes = "修改消费记录")
    public ResponseEntity<OrderConsume> updateOrderConsume(@RequestBody OrderConsume orderConsume){
        return ResponseEntity.ok(iOrderConsumeService.updateOrderConsume(orderConsume));
    }
    @GetMapping(value = "/query/one/{consumeId}")
    @ApiOperation(value = "查询消费记录", notes = "查询消费记录,按照记录编号")
    public ResponseEntity<OrderConsume> queryOrderConsume(
        @ApiParam(value = "记录编号",required = true,name = "consumeId")
        @PathVariable("consumeId") String consumeId){
        return ResponseEntity.ok(iOrderConsumeService.queryOrderConsume(consumeId));
    }

    @PostMapping(value = "/query/list")
    @ApiOperation(value = "查询消费记录列表", notes = "查询消费记录列表")
    public ResponseEntity<Map<String,Object>> queryOrderConsumeList(
        @RequestBody OrderConsume orderConsume,
        @RequestParam(defaultValue = "1") Integer pageNum,
        @RequestParam(defaultValue = "10") Integer pageSize){

        Map<String,Object> resultMap= Maps.newConcurrentMap();
        Page<OrderConsume> page = iOrderConsumeService.findStoreInfoListByPage(pageNum,pageSize,orderConsume);
        resultMap.put("result", page.getResult());
        resultMap.put("total", page.getTotal());
        return ResponseEntity.ok(resultMap);
    }

    @PostMapping(value = "query/list/cardNumber")
    @ApiOperation(value = "查询消费记录列表,获取会员卡余额", notes = "查询消费记录列表,按照会员卡余额")
    public ResponseEntity<Map<String,Object>> findOrderConsumeByCardNumber(
        @RequestBody OrderConsume orderConsume,
        @RequestParam("pageNum")Integer pageNum,
        @RequestParam("pageSize")Integer pageSize){
        Map<String,Object> result = iOrderConsumeService.findOrderConsumeByCardNumberAndBalance(pageNum,pageSize,orderConsume);
        return ResponseEntity.ok(result);
    }

    @PostMapping(value = "/add/batch", produces = {MediaType.APPLICATION_JSON_VALUE})
    @ApiOperation(value = "批量新增消费记录", notes = "批量新增消费记录")
    public ResponseEntity<Void> addOrderConsumeList(
        @RequestBody AddOrderConsumeReqDto addOrderConsumeReqDto) {
            iOrderConsumeService.addOrderConsumeList(addOrderConsumeReqDto);
        return ResponseEntity.ok().build();
    }
}
